﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using org.apache.zookeeper;
using ZooKeeperHelper.Enum;

namespace ZooKeeperHelper
{
    public class TaskHelper
    {
        private readonly ILog _log;
        //private int forcerun = Convert.ToInt32(ConfigurationManager.AppSettings["forcerun"]);
        //private string servicename = ConfigurationManager.AppSettings["servicename"];
        //private string pathname = ConfigurationManager.AppSettings["pathname"];
        private int forcerun = 5;
        private string servicename ="test";
        private string pathname = "Config";
        private ZooKeeperHelper _zooKeeperHelper;
        private ZooKeeper _zooKeeper;
        //单例输出，否则通知过多可能导致内存溢出
        private static TaskHelper _taskHelper;
        private static object _obj = new object();


        private TaskHelper(ILog log, int sessionTimeOut = 10 * 1000)
        {
            _log = log;
            _zooKeeperHelper = new ZooKeeperHelper(_log, sessionTimeOut);
        }


        public static TaskHelper GetInstance(ILog log, int sessionTimeOut = 10 * 1000)
        {
            if (_taskHelper == null)
            {
                lock (_obj)
                {
                    if (_taskHelper == null)
                    {
                        _taskHelper = new TaskHelper(log, sessionTimeOut);
                    }
                }
            }
            return _taskHelper;
        }


        public bool Return()
        {
            if (forcerun != 1)
            {
                try
                {
                    if (!_zooKeeperHelper.Connected())
                    {
                        _zooKeeper = _zooKeeperHelper.Connect(AuthEnum.World, "");
                        if (_zooKeeper == null)
                        {
                            _log.ErrorFormat("连接zooKeeper失败，时间是：{0}", DateTime.Now);
                            return true;
                        }
                    }
                    string path = ("/" + pathname);
                    string data = servicename;
                    //var aa = _zooKeeperHelper.GetData("/Config/childone");
                    string str = _zooKeeperHelper.ExistsNode(path, new NodeWatcher(_log));
                    if (str != "success")
                    {
                        str = _zooKeeperHelper.CreateNode(path, data);
                        if (str != path)
                        {
                            _log.ErrorFormat("创建路径失败，时间是：{0}", DateTime.Now);
                            return true;
                        }
                    }


                    string lockname = _zooKeeperHelper.GetData(path, new NodeWatcher(_log));


                    #region 测试通知
                    //string cg = _zooKeeperHelper.SetData(path, "hahhahahah");
                    //cg = _zooKeeperHelper.GetData(path, new NodeWatcher(_log));
                    //cg = _zooKeeperHelper.SetData(path, "1111111111");
                    //cg = _zooKeeperHelper.GetData(path, new NodeWatcher(_log));
                    //cg = _zooKeeperHelper.DeleteNode(path);
                    #endregion


                    //执行标识
                    if (lockname != servicename)
                    {
                        _log.ErrorFormat("非工作时间，当前执行的服务是：{0}，时间是：{1}", lockname, DateTime.Now);
                        return true;
                    }
                }
                catch (Exception exception)
                {
                    _log.ErrorFormat("zooKeeperHelper出现异常：{0}，时间是：{1}", exception.Message + exception.StackTrace, DateTime.Now);
                }
            }
            return false;
        }
    }

}
